package xyf.p11.other;

/**
 * ClassName: M1604_Tictactoe
 * Description:
 * Author: xyf
 * Date: 9/18/22 12:04 PM
 * Version: 1.0
 **/
public class M1604_Tictactoe
{
    public String tictactoe(String[] board)
    {
        int n = board.length;

        int[] rowX = new int[n];
        int[] rowO = new int[n];

        int[] colX = new int[n];
        int[] colO = new int[n];
        int duiX1 = 0;
        int duiO1 = 0;
        int duiX2 = 0;
        int duiO2 = 0;
        boolean hasSpace = false;

        for (int i = 0; i < board.length; i++)
        {
            for (int j = 0; j < board[i].length(); j++)
            {
                if (board[i].charAt(j) == 'X')
                {
                    rowX[i] += 1;
                    colX[j] += 1;
                    if (i == j)
                    {
                        duiX1 += 1;
                    }
                    if (i + j == n - 1)
                    {
                        duiX2 += 1;
                    }
                }
                else if (board[i].charAt(j) == 'O')
                {
                    rowO[i] += 1;
                    colO[j] += 1;
                    if (i == j)
                    {
                        duiO1 += 1;
                    }
                    if (i + j == n - 1)
                    {
                        duiO2 += 1;
                    }
                }
                else
                {
                    hasSpace = true;
                }

            }
        }

        for (int i = 0; i < rowX.length; i++)
        {
            if (rowX[i] == n || colX[i] == n)
            {
                return "X";
            }
            if (rowO[i] == n || colO[i] == n)
            {
                return "O";
            }
        }

        if (duiX1 == n || duiX2 == n)
        {
            return "X";
        }
        if (duiO1 == n || duiO2 == n)
        {
            return "O";
        }

        return hasSpace ? "Pending" : "Draw";
    }
}